/**
 * Alipay.com Inc. Copyright (c) 2004-2018 All Rights Reserved.
 */
package com.yeyks.model.Ajax;

import com.yeyks.model.em.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

/**
 * 通用请求返回结构，用它来包一层返回数据
 * @author Beacon
 * @version $Id: AjaxResponseBody.java, v 0.1 2018年07月30日 17:21 Beacon Exp $
 */
public class AjaxResponseBody {
    private int code;
    private String msg;
    private Object body;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public AjaxResponseBody addMapBody(String key, Object value) {
        if (body == null || !(body instanceof Map)) {
            body = new HashMap<>();
        }

        ((Map)body).put(key, value);
        return this;
    }

    public static AjaxResponseBody success(Object o) {
        AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
        ajaxResponseBody.setBody(o);
        ajaxResponseBody.setCode(0);
        ajaxResponseBody.setMsg("success");
        return ajaxResponseBody;
    }

    public static AjaxResponseBody success() {
        AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
        ajaxResponseBody.setCode(0);
        ajaxResponseBody.setMsg("success");
        return ajaxResponseBody;
    }

    public static AjaxResponseBody failed(String errorMsg) {
        AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
        ajaxResponseBody.setCode(-1);
        ajaxResponseBody.setMsg(errorMsg);
        return ajaxResponseBody;
    }

    public static AjaxResponseBody failed(Integer code, String errorMsg) {
        AjaxResponseBody ajaxResponseBody = new AjaxResponseBody();
        ajaxResponseBody.setCode(code);
        ajaxResponseBody.setMsg(errorMsg);
        return ajaxResponseBody;
    }
}